﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Terraria.DataStructures;
using Terraria.GameContent.Events;
using Terraria.GameContent.UI;
using Terraria.ID;
using Terraria.Localization;
using Terraria.ModLoader;

namespace Terraria
{
	// Token: 0x02000061 RID: 97
	public static class Wiring
	{
		// Token: 0x060007CB RID: 1995 RVA: 0x0000A2CB File Offset: 0x000084CB
		public static void SetCurrentUser(int plr = -1)
		{
			if (plr < 0 || plr >= 255)
			{
				plr = 254;
			}
			if (Main.netMode == 0)
			{
				plr = Main.myPlayer;
			}
			Wiring.CurrentUser = plr;
		}

		// Token: 0x060007CC RID: 1996 RVA: 0x00371A0C File Offset: 0x0036FC0C
		public static void Initialize()
		{
			Wiring._wireSkip = new Dictionary<Point16, bool>();
			Wiring._wireList = new DoubleStack<Point16>(1024, 0);
			Wiring._wireDirectionList = new DoubleStack<byte>(1024, 0);
			Wiring._toProcess = new Dictionary<Point16, byte>();
			Wiring._GatesCurrent = new Queue<Point16>();
			Wiring._GatesNext = new Queue<Point16>();
			Wiring._GatesDone = new Dictionary<Point16, bool>();
			Wiring._LampsToCheck = new Queue<Point16>();
			Wiring._PixelBoxTriggers = new Dictionary<Point16, byte>();
			Wiring._inPumpX = new int[20];
			Wiring._inPumpY = new int[20];
			Wiring._outPumpX = new int[20];
			Wiring._outPumpY = new int[20];
			Wiring._teleport = new Vector2[2];
			Wiring._mechX = new int[1000];
			Wiring._mechY = new int[1000];
			Wiring._mechTime = new int[1000];
		}

		// Token: 0x060007CD RID: 1997 RVA: 0x0000A2F4 File Offset: 0x000084F4
		public static void SkipWire(int x, int y)
		{
			Wiring._wireSkip[new Point16(x, y)] = true;
		}

		// Token: 0x060007CE RID: 1998 RVA: 0x0000A308 File Offset: 0x00008508
		public static void SkipWire(Point16 point)
		{
			Wiring._wireSkip[point] = true;
		}

		// Token: 0x060007CF RID: 1999 RVA: 0x00371AE8 File Offset: 0x0036FCE8
		public static void UpdateMech()
		{
			Wiring.SetCurrentUser(-1);
			for (int i = Wiring._numMechs - 1; i >= 0; i--)
			{
				Wiring._mechTime[i]--;
				if (Main.tile[Wiring._mechX[i], Wiring._mechY[i]].active() && Main.tile[Wiring._mechX[i], Wiring._mechY[i]].type == 144)
				{
					if (Main.tile[Wiring._mechX[i], Wiring._mechY[i]].frameY == 0)
					{
						Wiring._mechTime[i] = 0;
					}
					else
					{
						int num = (int)(Main.tile[Wiring._mechX[i], Wiring._mechY[i]].frameX / 18);
						if (num == 0)
						{
							num = 60;
						}
						else if (num == 1)
						{
							num = 180;
						}
						else if (num == 2)
						{
							num = 300;
						}
						if (Math.IEEERemainder((double)Wiring._mechTime[i], (double)num) == 0.0)
						{
							Wiring._mechTime[i] = 18000;
							Wiring.TripWire(Wiring._mechX[i], Wiring._mechY[i], 1, 1);
						}
					}
				}
				if (Wiring._mechTime[i] <= 0)
				{
					if (Main.tile[Wiring._mechX[i], Wiring._mechY[i]].active() && Main.tile[Wiring._mechX[i], Wiring._mechY[i]].type == 144)
					{
						Main.tile[Wiring._mechX[i], Wiring._mechY[i]].frameY = 0;
						NetMessage.SendTileSquare(-1, Wiring._mechX[i], Wiring._mechY[i], 1, TileChangeType.None);
					}
					if (Main.tile[Wiring._mechX[i], Wiring._mechY[i]].active() && Main.tile[Wiring._mechX[i], Wiring._mechY[i]].type == 411)
					{
						Tile tile = Main.tile[Wiring._mechX[i], Wiring._mechY[i]];
						int num2 = (int)(tile.frameX % 36 / 18);
						int num3 = (int)(tile.frameY % 36 / 18);
						int num4 = Wiring._mechX[i] - num2;
						int num5 = Wiring._mechY[i] - num3;
						int num6 = 36;
						if (Main.tile[num4, num5].frameX >= 36)
						{
							num6 = -36;
						}
						for (int j = num4; j < num4 + 2; j++)
						{
							for (int k = num5; k < num5 + 2; k++)
							{
								Main.tile[j, k].frameX = (short)((int)Main.tile[j, k].frameX + num6);
							}
						}
						NetMessage.SendTileSquare(-1, num4, num5, 2, TileChangeType.None);
					}
					for (int l = i; l < Wiring._numMechs; l++)
					{
						Wiring._mechX[l] = Wiring._mechX[l + 1];
						Wiring._mechY[l] = Wiring._mechY[l + 1];
						Wiring._mechTime[l] = Wiring._mechTime[l + 1];
					}
					Wiring._numMechs--;
				}
			}
		}

		// Token: 0x060007D0 RID: 2000 RVA: 0x00371DFC File Offset: 0x0036FFFC
		public static void HitSwitch(int i, int j)
		{
			if (!WorldGen.InWorld(i, j, 0))
			{
				return;
			}
			if (Main.tile[i, j] == null)
			{
				return;
			}
			if (Main.tile[i, j].type == 135 || Main.tile[i, j].type == 314 || Main.tile[i, j].type == 423 || Main.tile[i, j].type == 428 || Main.tile[i, j].type == 442)
			{
				Main.PlaySound(28, i * 16, j * 16, 0, 1f, 0f);
				Wiring.TripWire(i, j, 1, 1);
				return;
			}
			if (Main.tile[i, j].type == 440)
			{
				Main.PlaySound(28, i * 16 + 16, j * 16 + 16, 0, 1f, 0f);
				Wiring.TripWire(i, j, 3, 3);
				return;
			}
			if (Main.tile[i, j].type == 136)
			{
				if (Main.tile[i, j].frameY == 0)
				{
					Main.tile[i, j].frameY = 18;
				}
				else
				{
					Main.tile[i, j].frameY = 0;
				}
				Main.PlaySound(28, i * 16, j * 16, 0, 1f, 0f);
				Wiring.TripWire(i, j, 1, 1);
				return;
			}
			if (Main.tile[i, j].type == 144)
			{
				if (Main.tile[i, j].frameY == 0)
				{
					Main.tile[i, j].frameY = 18;
					if (Main.netMode != 1)
					{
						Wiring.CheckMech(i, j, 18000);
					}
				}
				else
				{
					Main.tile[i, j].frameY = 0;
				}
				Main.PlaySound(28, i * 16, j * 16, 0, 1f, 0f);
				return;
			}
			if (Main.tile[i, j].type == 441 || Main.tile[i, j].type == 468)
			{
				int num = (int)(Main.tile[i, j].frameX / 18 * -1);
				int num2 = (int)(Main.tile[i, j].frameY / 18 * -1);
				num %= 4;
				if (num < -1)
				{
					num += 2;
				}
				num += i;
				num2 += j;
				Main.PlaySound(28, i * 16, j * 16, 0, 1f, 0f);
				Wiring.TripWire(num, num2, 2, 2);
				return;
			}
			if (Main.tile[i, j].type == 132 || Main.tile[i, j].type == 411)
			{
				short num3 = 36;
				int num4 = (int)(Main.tile[i, j].frameX / 18 * -1);
				int num5 = (int)(Main.tile[i, j].frameY / 18 * -1);
				num4 %= 4;
				if (num4 < -1)
				{
					num4 += 2;
					num3 = -36;
				}
				num4 += i;
				num5 += j;
				if (Main.netMode != 1 && Main.tile[num4, num5].type == 411)
				{
					Wiring.CheckMech(num4, num5, 60);
				}
				for (int k = num4; k < num4 + 2; k++)
				{
					for (int l = num5; l < num5 + 2; l++)
					{
						if (Main.tile[k, l].type == 132 || Main.tile[k, l].type == 411)
						{
							Tile tile = Main.tile[k, l];
							Tile tile2 = tile;
							tile2.frameX += num3;
						}
					}
				}
				WorldGen.TileFrame(num4, num5, false, false);
				Main.PlaySound(28, i * 16, j * 16, 0, 1f, 0f);
				Wiring.TripWire(num4, num5, 2, 2);
			}
		}

		// Token: 0x060007D1 RID: 2001 RVA: 0x0000A316 File Offset: 0x00008516
		public static void PokeLogicGate(int lampX, int lampY)
		{
			if (Main.netMode == 1)
			{
				return;
			}
			Wiring._LampsToCheck.Enqueue(new Point16(lampX, lampY));
			Wiring.LogicGatePass();
		}

		// Token: 0x060007D2 RID: 2002 RVA: 0x003721E8 File Offset: 0x003703E8
		public static bool Actuate(int i, int j)
		{
			Tile tile = Main.tile[i, j];
			if (!tile.actuator())
			{
				return false;
			}
			if ((tile.type != 226 || (double)j <= Main.worldSurface || NPC.downedPlantBoss) && ((double)j <= Main.worldSurface || NPC.downedGolemBoss || Main.tile[i, j - 1].type != 237))
			{
				if (tile.inActive())
				{
					Wiring.ReActive(i, j);
				}
				else
				{
					Wiring.DeActive(i, j);
				}
			}
			return true;
		}

		// Token: 0x060007D3 RID: 2003 RVA: 0x0037226C File Offset: 0x0037046C
		public static void ActuateForced(int i, int j)
		{
			Tile tile = Main.tile[i, j];
			if (tile.type == 226 && (double)j > Main.worldSurface && !NPC.downedPlantBoss)
			{
				return;
			}
			if (tile.inActive())
			{
				Wiring.ReActive(i, j);
				return;
			}
			Wiring.DeActive(i, j);
		}

		// Token: 0x060007D4 RID: 2004 RVA: 0x003722BC File Offset: 0x003704BC
		public static void MassWireOperation(Point ps, Point pe, Player master)
		{
			int num = 0;
			int num2 = 0;
			for (int i = 0; i < 58; i++)
			{
				if (master.inventory[i].type == 530)
				{
					num += master.inventory[i].stack;
				}
				if (master.inventory[i].type == 849)
				{
					num2 += master.inventory[i].stack;
				}
			}
			int num3 = num;
			int num4 = num2;
			Wiring.MassWireOperationInner(ps, pe, master.Center, master.direction == 1, ref num, ref num2);
			int num5 = num3 - num;
			int num6 = num4 - num2;
			if (Main.netMode == 2)
			{
				NetMessage.SendData(110, master.whoAmI, -1, null, 530, (float)num5, (float)master.whoAmI, 0f, 0, 0, 0);
				NetMessage.SendData(110, master.whoAmI, -1, null, 849, (float)num6, (float)master.whoAmI, 0f, 0, 0, 0);
				return;
			}
			for (int j = 0; j < num5; j++)
			{
				master.ConsumeItem(530, false);
			}
			for (int k = 0; k < num6; k++)
			{
				master.ConsumeItem(849, false);
			}
		}

		// Token: 0x060007D5 RID: 2005 RVA: 0x003723E4 File Offset: 0x003705E4
		public static bool CheckMech(int i, int j, int time)
		{
			for (int k = 0; k < Wiring._numMechs; k++)
			{
				if (Wiring._mechX[k] == i && Wiring._mechY[k] == j)
				{
					return false;
				}
			}
			if (Wiring._numMechs < 999)
			{
				Wiring._mechX[Wiring._numMechs] = i;
				Wiring._mechY[Wiring._numMechs] = j;
				Wiring._mechTime[Wiring._numMechs] = time;
				Wiring._numMechs++;
				return true;
			}
			return false;
		}

		// Token: 0x060007D6 RID: 2006 RVA: 0x00372458 File Offset: 0x00370658
		private static void XferWater()
		{
			for (int i = 0; i < Wiring._numInPump; i++)
			{
				int num = Wiring._inPumpX[i];
				int num2 = Wiring._inPumpY[i];
				int liquid = (int)Main.tile[num, num2].liquid;
				if (liquid > 0)
				{
					bool flag = Main.tile[num, num2].lava();
					bool flag2 = Main.tile[num, num2].honey();
					for (int j = 0; j < Wiring._numOutPump; j++)
					{
						int num3 = Wiring._outPumpX[j];
						int num4 = Wiring._outPumpY[j];
						int liquid2 = (int)Main.tile[num3, num4].liquid;
						if (liquid2 < 255)
						{
							bool flag3 = Main.tile[num3, num4].lava();
							bool flag4 = Main.tile[num3, num4].honey();
							if (liquid2 == 0)
							{
								flag3 = flag;
								flag4 = flag2;
							}
							if (flag == flag3 && flag2 == flag4)
							{
								int num5 = liquid;
								if (num5 + liquid2 > 255)
								{
									num5 = 255 - liquid2;
								}
								Tile tile = Main.tile[num3, num4];
								Tile tile2 = tile;
								tile2.liquid += (byte)num5;
								Tile tile3 = Main.tile[num, num2];
								Tile tile4 = tile3;
								tile4.liquid -= (byte)num5;
								liquid = (int)Main.tile[num, num2].liquid;
								Main.tile[num3, num4].lava(flag);
								Main.tile[num3, num4].honey(flag2);
								WorldGen.SquareTileFrame(num3, num4, true);
								if (Main.tile[num, num2].liquid == 0)
								{
									Main.tile[num, num2].lava(false);
									WorldGen.SquareTileFrame(num, num2, true);
									break;
								}
							}
						}
					}
					WorldGen.SquareTileFrame(num, num2, true);
				}
			}
		}

		// Token: 0x060007D7 RID: 2007 RVA: 0x0037263C File Offset: 0x0037083C
		public static void TripWire(int left, int top, int width, int height)
		{
			if (Main.netMode == 1)
			{
				return;
			}
			Wiring.running = true;
			if (Wiring._wireList.Count != 0)
			{
				Wiring._wireList.Clear(true);
			}
			if (Wiring._wireDirectionList.Count != 0)
			{
				Wiring._wireDirectionList.Clear(true);
			}
			Vector2[] array = new Vector2[8];
			int num = 0;
			for (int i = left; i < left + width; i++)
			{
				for (int j = top; j < top + height; j++)
				{
					Point16 back = new Point16(i, j);
					Tile tile = Main.tile[i, j];
					if (tile != null && tile.wire())
					{
						Wiring._wireList.PushBack(back);
					}
				}
			}
			Wiring._teleport[0].X = -1f;
			Wiring._teleport[0].Y = -1f;
			Wiring._teleport[1].X = -1f;
			Wiring._teleport[1].Y = -1f;
			if (Wiring._wireList.Count > 0)
			{
				Wiring._numInPump = 0;
				Wiring._numOutPump = 0;
				Wiring.HitWire(Wiring._wireList, 1);
				if (Wiring._numInPump > 0 && Wiring._numOutPump > 0)
				{
					Wiring.XferWater();
				}
			}
			array[num++] = Wiring._teleport[0];
			array[num++] = Wiring._teleport[1];
			for (int k = left; k < left + width; k++)
			{
				for (int l = top; l < top + height; l++)
				{
					Point16 back2 = new Point16(k, l);
					Tile tile2 = Main.tile[k, l];
					if (tile2 != null && tile2.wire2())
					{
						Wiring._wireList.PushBack(back2);
					}
				}
			}
			Wiring._teleport[0].X = -1f;
			Wiring._teleport[0].Y = -1f;
			Wiring._teleport[1].X = -1f;
			Wiring._teleport[1].Y = -1f;
			if (Wiring._wireList.Count > 0)
			{
				Wiring._numInPump = 0;
				Wiring._numOutPump = 0;
				Wiring.HitWire(Wiring._wireList, 2);
				if (Wiring._numInPump > 0 && Wiring._numOutPump > 0)
				{
					Wiring.XferWater();
				}
			}
			array[num++] = Wiring._teleport[0];
			array[num++] = Wiring._teleport[1];
			Wiring._teleport[0].X = -1f;
			Wiring._teleport[0].Y = -1f;
			Wiring._teleport[1].X = -1f;
			Wiring._teleport[1].Y = -1f;
			for (int m = left; m < left + width; m++)
			{
				for (int n = top; n < top + height; n++)
				{
					Point16 back3 = new Point16(m, n);
					Tile tile3 = Main.tile[m, n];
					if (tile3 != null && tile3.wire3())
					{
						Wiring._wireList.PushBack(back3);
					}
				}
			}
			if (Wiring._wireList.Count > 0)
			{
				Wiring._numInPump = 0;
				Wiring._numOutPump = 0;
				Wiring.HitWire(Wiring._wireList, 3);
				if (Wiring._numInPump > 0 && Wiring._numOutPump > 0)
				{
					Wiring.XferWater();
				}
			}
			array[num++] = Wiring._teleport[0];
			array[num++] = Wiring._teleport[1];
			Wiring._teleport[0].X = -1f;
			Wiring._teleport[0].Y = -1f;
			Wiring._teleport[1].X = -1f;
			Wiring._teleport[1].Y = -1f;
			for (int num2 = left; num2 < left + width; num2++)
			{
				for (int num3 = top; num3 < top + height; num3++)
				{
					Point16 back4 = new Point16(num2, num3);
					Tile tile4 = Main.tile[num2, num3];
					if (tile4 != null && tile4.wire4())
					{
						Wiring._wireList.PushBack(back4);
					}
				}
			}
			if (Wiring._wireList.Count > 0)
			{
				Wiring._numInPump = 0;
				Wiring._numOutPump = 0;
				Wiring.HitWire(Wiring._wireList, 4);
				if (Wiring._numInPump > 0 && Wiring._numOutPump > 0)
				{
					Wiring.XferWater();
				}
			}
			array[num++] = Wiring._teleport[0];
			array[num++] = Wiring._teleport[1];
			for (int num4 = 0; num4 < 8; num4 += 2)
			{
				Wiring._teleport[0] = array[num4];
				Wiring._teleport[1] = array[num4 + 1];
				if (Wiring._teleport[0].X >= 0f && Wiring._teleport[1].X >= 0f)
				{
					Wiring.Teleport();
				}
			}
			Wiring.PixelBoxPass();
			Wiring.LogicGatePass();
		}

		// Token: 0x060007D8 RID: 2008 RVA: 0x00372B3C File Offset: 0x00370D3C
		private static void PixelBoxPass()
		{
			foreach (KeyValuePair<Point16, byte> keyValuePair in Wiring._PixelBoxTriggers)
			{
				if (keyValuePair.Value != 2)
				{
					if (keyValuePair.Value == 1)
					{
						if (Main.tile[(int)keyValuePair.Key.X, (int)keyValuePair.Key.Y].frameX != 0)
						{
							Main.tile[(int)keyValuePair.Key.X, (int)keyValuePair.Key.Y].frameX = 0;
							NetMessage.SendTileSquare(-1, (int)keyValuePair.Key.X, (int)keyValuePair.Key.Y, 1, TileChangeType.None);
						}
					}
					else if (keyValuePair.Value == 3 && Main.tile[(int)keyValuePair.Key.X, (int)keyValuePair.Key.Y].frameX != 18)
					{
						Main.tile[(int)keyValuePair.Key.X, (int)keyValuePair.Key.Y].frameX = 18;
						NetMessage.SendTileSquare(-1, (int)keyValuePair.Key.X, (int)keyValuePair.Key.Y, 1, TileChangeType.None);
					}
				}
			}
			Wiring._PixelBoxTriggers.Clear();
		}

		// Token: 0x060007D9 RID: 2009 RVA: 0x00372CB0 File Offset: 0x00370EB0
		private static void LogicGatePass()
		{
			if (Wiring._GatesCurrent.Count == 0)
			{
				Wiring._GatesDone.Clear();
				while (Wiring._LampsToCheck.Count > 0)
				{
					while (Wiring._LampsToCheck.Count > 0)
					{
						Point16 point = Wiring._LampsToCheck.Dequeue();
						Wiring.CheckLogicGate((int)point.X, (int)point.Y);
					}
					while (Wiring._GatesNext.Count > 0)
					{
						Utils.Swap<Queue<Point16>>(ref Wiring._GatesCurrent, ref Wiring._GatesNext);
						while (Wiring._GatesCurrent.Count > 0)
						{
							Point16 point2 = Wiring._GatesCurrent.Peek();
							bool flag;
							if (Wiring._GatesDone.TryGetValue(point2, out flag) && flag)
							{
								Wiring._GatesCurrent.Dequeue();
							}
							else
							{
								Wiring._GatesDone.Add(point2, true);
								Wiring.TripWire((int)point2.X, (int)point2.Y, 1, 1);
								Wiring._GatesCurrent.Dequeue();
							}
						}
					}
				}
				Wiring._GatesDone.Clear();
				if (Wiring.blockPlayerTeleportationForOneIteration)
				{
					Wiring.blockPlayerTeleportationForOneIteration = false;
				}
			}
		}

		// Token: 0x060007DA RID: 2010 RVA: 0x00372DAC File Offset: 0x00370FAC
		private static void CheckLogicGate(int lampX, int lampY)
		{
			if (!WorldGen.InWorld(lampX, lampY, 1))
			{
				return;
			}
			int i = lampY;
			while (i < Main.maxTilesY)
			{
				Tile tile = Main.tile[lampX, i];
				if (!tile.active())
				{
					return;
				}
				if (tile.type == 420)
				{
					bool flag;
					Wiring._GatesDone.TryGetValue(new Point16(lampX, i), out flag);
					int num = (int)(tile.frameY / 18);
					bool flag2 = tile.frameX == 18;
					bool flag3 = tile.frameX == 36;
					if (num < 0)
					{
						return;
					}
					int num2 = 0;
					int num3 = 0;
					bool flag4 = false;
					for (int j = i - 1; j > 0; j--)
					{
						Tile tile2 = Main.tile[lampX, j];
						if (!tile2.active() || tile2.type != 419)
						{
							break;
						}
						if (tile2.frameX == 36)
						{
							flag4 = true;
							break;
						}
						num2++;
						num3 += (tile2.frameX == 18).ToInt();
					}
					bool flag5;
					switch (num)
					{
					case 0:
						flag5 = (num2 == num3);
						break;
					case 1:
						flag5 = (num3 > 0);
						break;
					case 2:
						flag5 = (num2 != num3);
						break;
					case 3:
						flag5 = (num3 == 0);
						break;
					case 4:
						flag5 = (num3 == 1);
						break;
					case 5:
						flag5 = (num3 != 1);
						break;
					default:
						return;
					}
					bool flag6 = !flag4 && flag3;
					bool flag7 = false;
					if (flag4 && Framing.GetTileSafely(lampX, lampY).frameX == 36)
					{
						flag7 = true;
					}
					if (flag5 != flag2 || flag6 || flag7)
					{
						short num4 = tile.frameX % 18 / 18;
						tile.frameX = (short)(18 * flag5.ToInt());
						if (flag4)
						{
							tile.frameX = 36;
						}
						Wiring.SkipWire(lampX, i);
						WorldGen.SquareTileFrame(lampX, i, true);
						NetMessage.SendTileSquare(-1, lampX, i, 1, TileChangeType.None);
						bool flag8 = !flag4 || flag7;
						if (flag7)
						{
							if (num3 != 0)
							{
							}
							flag8 = (Main.rand.NextFloat() < (float)num3 / (float)num2);
						}
						if (flag6)
						{
							flag8 = false;
						}
						if (flag8)
						{
							if (!flag)
							{
								Wiring._GatesNext.Enqueue(new Point16(lampX, i));
								return;
							}
							Vector2 vector = new Vector2((float)lampX, (float)i) * 16f - new Vector2(10f);
							Utils.PoofOfSmoke(vector);
							NetMessage.SendData(106, -1, -1, null, (int)vector.X, vector.Y, 0f, 0f, 0, 0, 0);
						}
					}
					return;
				}
				else
				{
					if (tile.type != 419)
					{
						return;
					}
					i++;
				}
			}
		}

		// Token: 0x060007DB RID: 2011 RVA: 0x0037302C File Offset: 0x0037122C
		private static void HitWire(DoubleStack<Point16> next, int wireType)
		{
			Wiring._wireDirectionList.Clear(true);
			for (int i = 0; i < next.Count; i++)
			{
				Point16 point = next.PopFront();
				Wiring.SkipWire(point);
				Wiring._toProcess.Add(point, 4);
				next.PushBack(point);
				Wiring._wireDirectionList.PushBack(0);
			}
			Wiring._currentWireColor = wireType;
			while (next.Count > 0)
			{
				Point16 point2 = next.PopFront();
				int num = (int)Wiring._wireDirectionList.PopFront();
				int x = (int)point2.X;
				int y = (int)point2.Y;
				if (!Wiring._wireSkip.ContainsKey(point2))
				{
					Wiring.HitWireSingle(x, y);
				}
				for (int j = 0; j < 4; j++)
				{
					int num2;
					int num3;
					switch (j)
					{
					case 0:
						num2 = x;
						num3 = y + 1;
						break;
					case 1:
						num2 = x;
						num3 = y - 1;
						break;
					case 2:
						num2 = x + 1;
						num3 = y;
						break;
					case 3:
						num2 = x - 1;
						num3 = y;
						break;
					default:
						num2 = x;
						num3 = y + 1;
						break;
					}
					if (num2 >= 2 && num2 < Main.maxTilesX - 2 && num3 >= 2 && num3 < Main.maxTilesY - 2)
					{
						Tile tile = Main.tile[num2, num3];
						if (tile != null)
						{
							Tile tile2 = Main.tile[x, y];
							if (tile2 != null)
							{
								byte b = 3;
								if (tile.type == 424 || tile.type == 445)
								{
									b = 0;
								}
								if (tile2.type == 424)
								{
									switch (tile2.frameX / 18)
									{
									case 0:
										if (j != num)
										{
											goto IL_315;
										}
										break;
									case 1:
										if ((num != 0 || j != 3) && (num != 3 || j != 0) && (num != 1 || j != 2))
										{
											if (num != 2)
											{
												goto IL_315;
											}
											if (j != 1)
											{
												goto IL_315;
											}
										}
										break;
									case 2:
										if ((num != 0 || j != 2) && (num != 2 || j != 0) && (num != 1 || j != 3) && (num != 3 || j != 1))
										{
											goto IL_315;
										}
										break;
									}
								}
								if (tile2.type == 445)
								{
									if (j != num)
									{
										goto IL_315;
									}
									if (Wiring._PixelBoxTriggers.ContainsKey(point2))
									{
										Dictionary<Point16, byte> pixelBoxTriggers;
										Point16 key;
										(pixelBoxTriggers = Wiring._PixelBoxTriggers)[key = point2] = (pixelBoxTriggers[key] | ((j == 0 | j == 1) ? 2 : 1));
									}
									else
									{
										Wiring._PixelBoxTriggers[point2] = ((j == 0 | j == 1) ? 2 : 1);
									}
								}
								bool flag;
								switch (wireType)
								{
								case 1:
									flag = tile.wire();
									break;
								case 2:
									flag = tile.wire2();
									break;
								case 3:
									flag = tile.wire3();
									break;
								case 4:
									flag = tile.wire4();
									break;
								default:
									flag = false;
									break;
								}
								if (flag)
								{
									Point16 point3 = new Point16(num2, num3);
									byte b2;
									if (Wiring._toProcess.TryGetValue(point3, out b2))
									{
										b2 -= 1;
										if (b2 == 0)
										{
											Wiring._toProcess.Remove(point3);
										}
										else
										{
											Wiring._toProcess[point3] = b2;
										}
									}
									else
									{
										next.PushBack(point3);
										Wiring._wireDirectionList.PushBack((byte)j);
										if (b > 0)
										{
											Wiring._toProcess.Add(point3, b);
										}
									}
								}
							}
						}
					}
					IL_315:;
				}
			}
			Wiring._wireSkip.Clear();
			Wiring._toProcess.Clear();
			Wiring.running = false;
		}

		// Token: 0x060007DC RID: 2012 RVA: 0x00373384 File Offset: 0x00371584
		private static void HitWireSingle(int i, int j)
		{
			Tile tile = Main.tile[i, j];
			int type = (int)tile.type;
			if (tile.actuator())
			{
				Wiring.ActuateForced(i, j);
			}
			if (tile.active())
			{
				if (!TileLoader.PreHitWire(i, j, type))
				{
					return;
				}
				if (type == 144)
				{
					Wiring.HitSwitch(i, j);
					WorldGen.SquareTileFrame(i, j, true);
					NetMessage.SendTileSquare(-1, i, j, 1, TileChangeType.None);
				}
				else if (type == 421)
				{
					if (!tile.actuator())
					{
						tile.type = 422;
						WorldGen.SquareTileFrame(i, j, true);
						NetMessage.SendTileSquare(-1, i, j, 1, TileChangeType.None);
					}
				}
				else if (type == 422 && !tile.actuator())
				{
					tile.type = 421;
					WorldGen.SquareTileFrame(i, j, true);
					NetMessage.SendTileSquare(-1, i, j, 1, TileChangeType.None);
				}
				if (type >= 255 && type <= 268)
				{
					if (!tile.actuator())
					{
						if (type >= 262)
						{
							Tile tile2 = tile;
							Tile tile3 = tile2;
							tile3.type -= 7;
						}
						else
						{
							Tile tile4 = tile;
							Tile tile3 = tile4;
							tile3.type += 7;
						}
						WorldGen.SquareTileFrame(i, j, true);
						NetMessage.SendTileSquare(-1, i, j, 1, TileChangeType.None);
						return;
					}
				}
				else
				{
					if (type == 419)
					{
						int num = 18;
						if ((int)tile.frameX >= num)
						{
							num = -num;
						}
						if (tile.frameX == 36)
						{
							num = 0;
						}
						Wiring.SkipWire(i, j);
						tile.frameX = (short)((int)tile.frameX + num);
						WorldGen.SquareTileFrame(i, j, true);
						NetMessage.SendTileSquare(-1, i, j, 1, TileChangeType.None);
						Wiring._LampsToCheck.Enqueue(new Point16(i, j));
						return;
					}
					if (type == 406)
					{
						int num2 = (int)(tile.frameX % 54 / 18);
						int num3 = (int)(tile.frameY % 54 / 18);
						int num4 = i - num2;
						int num5 = j - num3;
						int num6 = 54;
						if (Main.tile[num4, num5].frameY >= 108)
						{
							num6 = -108;
						}
						int num7;
						for (int k = num4; k < num4 + 3; k = num7 + 1)
						{
							for (int l = num5; l < num5 + 3; l = num7 + 1)
							{
								Wiring.SkipWire(k, l);
								Main.tile[k, l].frameY = (short)((int)Main.tile[k, l].frameY + num6);
								num7 = l;
							}
							num7 = k;
						}
						NetMessage.SendTileSquare(-1, num4 + 1, num5 + 1, 3, TileChangeType.None);
						return;
					}
					if (type == 452)
					{
						int num8 = (int)(tile.frameX % 54 / 18);
						int num9 = (int)(tile.frameY % 54 / 18);
						int num10 = i - num8;
						int num11 = j - num9;
						int num12 = 54;
						if (Main.tile[num10, num11].frameX >= 54)
						{
							num12 = -54;
						}
						int num7;
						for (int m = num10; m < num10 + 3; m = num7 + 1)
						{
							for (int n = num11; n < num11 + 3; n = num7 + 1)
							{
								Wiring.SkipWire(m, n);
								Main.tile[m, n].frameX = (short)((int)Main.tile[m, n].frameX + num12);
								num7 = n;
							}
							num7 = m;
						}
						NetMessage.SendTileSquare(-1, num10 + 1, num11 + 1, 3, TileChangeType.None);
						return;
					}
					if (type == 411)
					{
						int num13 = (int)(tile.frameX % 36 / 18);
						int num14 = (int)(tile.frameY % 36 / 18);
						int num15 = i - num13;
						int num16 = j - num14;
						int num17 = 36;
						if (Main.tile[num15, num16].frameX >= 36)
						{
							num17 = -36;
						}
						int num7;
						for (int num18 = num15; num18 < num15 + 2; num18 = num7 + 1)
						{
							for (int num19 = num16; num19 < num16 + 2; num19 = num7 + 1)
							{
								Wiring.SkipWire(num18, num19);
								Main.tile[num18, num19].frameX = (short)((int)Main.tile[num18, num19].frameX + num17);
								num7 = num19;
							}
							num7 = num18;
						}
						NetMessage.SendTileSquare(-1, num15, num16, 2, TileChangeType.None);
						return;
					}
					if (type == 425)
					{
						int num20 = (int)(tile.frameX % 36 / 18);
						int num21 = (int)(tile.frameY % 36 / 18);
						int num22 = i - num20;
						int num23 = j - num21;
						int num7;
						for (int num24 = num22; num24 < num22 + 2; num24 = num7 + 1)
						{
							for (int num25 = num23; num25 < num23 + 2; num25 = num7 + 1)
							{
								Wiring.SkipWire(num24, num25);
								num7 = num25;
							}
							num7 = num24;
						}
						if (!Main.AnnouncementBoxDisabled)
						{
							Color pink = Color.Pink;
							int num26 = Sign.ReadSign(num22, num23, false);
							if (num26 != -1 && Main.sign[num26] != null && !string.IsNullOrWhiteSpace(Main.sign[num26].text))
							{
								if (Main.AnnouncementBoxRange == -1)
								{
									if (Main.netMode == 0)
									{
										Main.NewTextMultiline(Main.sign[num26].text, false, pink, 460);
										return;
									}
									if (Main.netMode == 2)
									{
										NetMessage.SendData(107, -1, -1, NetworkText.FromLiteral(Main.sign[num26].text), 255, (float)pink.R, (float)pink.G, (float)pink.B, 460, 0, 0);
										return;
									}
								}
								else if (Main.netMode == 0)
								{
									if (Main.player[Main.myPlayer].Distance(new Vector2((float)(num22 * 16 + 16), (float)(num23 * 16 + 16))) <= (float)Main.AnnouncementBoxRange)
									{
										Main.NewTextMultiline(Main.sign[num26].text, false, pink, 460);
										return;
									}
								}
								else if (Main.netMode == 2)
								{
									for (int num27 = 0; num27 < 255; num27 = num7 + 1)
									{
										if (Main.player[num27].active && Main.player[num27].Distance(new Vector2((float)(num22 * 16 + 16), (float)(num23 * 16 + 16))) <= (float)Main.AnnouncementBoxRange)
										{
											NetMessage.SendData(107, num27, -1, NetworkText.FromLiteral(Main.sign[num26].text), 255, (float)pink.R, (float)pink.G, (float)pink.B, 460, 0, 0);
										}
										num7 = num27;
									}
									return;
								}
							}
						}
					}
					else
					{
						if (type == 405)
						{
							int num28 = (int)(tile.frameX % 54 / 18);
							int num29 = (int)(tile.frameY % 36 / 18);
							int num30 = i - num28;
							int num31 = j - num29;
							int num32 = 54;
							if (Main.tile[num30, num31].frameX >= 54)
							{
								num32 = -54;
							}
							int num7;
							for (int num33 = num30; num33 < num30 + 3; num33 = num7 + 1)
							{
								for (int num34 = num31; num34 < num31 + 2; num34 = num7 + 1)
								{
									Wiring.SkipWire(num33, num34);
									Main.tile[num33, num34].frameX = (short)((int)Main.tile[num33, num34].frameX + num32);
									num7 = num34;
								}
								num7 = num33;
							}
							NetMessage.SendTileSquare(-1, num30 + 1, num31 + 1, 3, TileChangeType.None);
							return;
						}
						if (type == 209)
						{
							int num35 = (int)(tile.frameX % 72 / 18);
							int num36 = (int)(tile.frameY % 54 / 18);
							int num37 = i - num35;
							int num38 = j - num36;
							int num39 = (int)(tile.frameY / 54);
							int num40 = (int)(tile.frameX / 72);
							int num41 = -1;
							if (num35 == 1 || num35 == 2)
							{
								num41 = num36;
							}
							int num42 = 0;
							if (num35 == 3)
							{
								num42 = -54;
							}
							if (num35 == 0)
							{
								num42 = 54;
							}
							if (num39 >= 8 && num42 > 0)
							{
								num42 = 0;
							}
							if (num39 == 0 && num42 < 0)
							{
								num42 = 0;
							}
							bool flag = false;
							if (num42 != 0)
							{
								int num7;
								for (int num43 = num37; num43 < num37 + 4; num43 = num7 + 1)
								{
									for (int num44 = num38; num44 < num38 + 3; num44 = num7 + 1)
									{
										Wiring.SkipWire(num43, num44);
										Main.tile[num43, num44].frameY = (short)((int)Main.tile[num43, num44].frameY + num42);
										num7 = num44;
									}
									num7 = num43;
								}
								flag = true;
							}
							if ((num40 == 3 || num40 == 4) && (num41 == 0 || num41 == 1))
							{
								num42 = ((num40 == 3) ? 72 : -72);
								int num7;
								for (int num45 = num37; num45 < num37 + 4; num45 = num7 + 1)
								{
									for (int num46 = num38; num46 < num38 + 3; num46 = num7 + 1)
									{
										Wiring.SkipWire(num45, num46);
										Main.tile[num45, num46].frameX = (short)((int)Main.tile[num45, num46].frameX + num42);
										num7 = num46;
									}
									num7 = num45;
								}
								flag = true;
							}
							if (flag)
							{
								NetMessage.SendTileSquare(-1, num37 + 1, num38 + 1, 4, TileChangeType.None);
							}
							if (num41 != -1)
							{
								bool flag2 = true;
								if ((num40 == 3 || num40 == 4) && num41 < 2)
								{
									flag2 = false;
								}
								if (Wiring.CheckMech(num37, num38, 30) && flag2)
								{
									WorldGen.ShootFromCannon(num37, num38, num39, num40 + 1, 0, 0f, Wiring.CurrentUser);
									return;
								}
							}
						}
						else if (type == 212)
						{
							int num47 = (int)(tile.frameX % 54 / 18);
							int num48 = (int)(tile.frameY % 54 / 18);
							int num49 = i - num47;
							int num50 = j - num48;
							int num51 = (int)(tile.frameX / 54);
							int num52 = -1;
							if (num47 == 1)
							{
								num52 = num48;
							}
							int num53 = 0;
							if (num47 == 0)
							{
								num53 = -54;
							}
							if (num47 == 2)
							{
								num53 = 54;
							}
							if (num51 >= 1 && num53 > 0)
							{
								num53 = 0;
							}
							if (num51 == 0 && num53 < 0)
							{
								num53 = 0;
							}
							bool flag3 = false;
							if (num53 != 0)
							{
								int num7;
								for (int num54 = num49; num54 < num49 + 3; num54 = num7 + 1)
								{
									for (int num55 = num50; num55 < num50 + 3; num55 = num7 + 1)
									{
										Wiring.SkipWire(num54, num55);
										Main.tile[num54, num55].frameX = (short)((int)Main.tile[num54, num55].frameX + num53);
										num7 = num55;
									}
									num7 = num54;
								}
								flag3 = true;
							}
							if (flag3)
							{
								NetMessage.SendTileSquare(-1, num49 + 1, num50 + 1, 4, TileChangeType.None);
							}
							if (num52 != -1 && Wiring.CheckMech(num49, num50, 10))
							{
								float num56 = 12f + (float)Main.rand.Next(450) * 0.01f;
								float num57 = (float)Main.rand.Next(85, 105);
								float num58 = (float)Main.rand.Next(-35, 11);
								int type2 = 166;
								int damage = 0;
								float knockBack = 0f;
								Vector2 vector = new Vector2((float)((num49 + 2) * 16 - 8), (float)((num50 + 2) * 16 - 8));
								if (tile.frameX / 54 == 0)
								{
									num57 *= -1f;
									vector.X -= 12f;
								}
								else
								{
									vector.X += 12f;
								}
								float num59 = num57;
								float num60 = num58;
								float num61 = (float)Math.Sqrt((double)(num59 * num59 + num60 * num60));
								num61 = num56 / num61;
								num59 *= num61;
								num60 *= num61;
								Projectile.NewProjectile(vector.X, vector.Y, num59, num60, type2, damage, knockBack, Wiring.CurrentUser, 0f, 0f);
								return;
							}
						}
						else
						{
							if (type == 215)
							{
								int num62 = (int)(tile.frameX % 54 / 18);
								int num63 = (int)(tile.frameY % 36 / 18);
								int num64 = i - num62;
								int num65 = j - num63;
								int num66 = 36;
								if (Main.tile[num64, num65].frameY >= 36)
								{
									num66 = -36;
								}
								int num7;
								for (int num67 = num64; num67 < num64 + 3; num67 = num7 + 1)
								{
									for (int num68 = num65; num68 < num65 + 2; num68 = num7 + 1)
									{
										Wiring.SkipWire(num67, num68);
										Main.tile[num67, num68].frameY = (short)((int)Main.tile[num67, num68].frameY + num66);
										num7 = num68;
									}
									num7 = num67;
								}
								NetMessage.SendTileSquare(-1, num64 + 1, num65 + 1, 3, TileChangeType.None);
								return;
							}
							if (type == 130)
							{
								if (Main.tile[i, j - 1] != null && Main.tile[i, j - 1].active())
								{
									if (TileID.Sets.BasicChest[(int)Main.tile[i, j - 1].type] || TileID.Sets.BasicChestFake[(int)Main.tile[i, j - 1].type])
									{
										return;
									}
									if (Main.tile[i, j - 1].type == 88)
									{
										return;
									}
								}
								tile.type = 131;
								WorldGen.SquareTileFrame(i, j, true);
								NetMessage.SendTileSquare(-1, i, j, 1, TileChangeType.None);
								return;
							}
							if (type == 131)
							{
								tile.type = 130;
								WorldGen.SquareTileFrame(i, j, true);
								NetMessage.SendTileSquare(-1, i, j, 1, TileChangeType.None);
								return;
							}
							if (type == 387 || type == 386)
							{
								bool value = type == 387;
								int num69 = WorldGen.ShiftTrapdoor(i, j, true, -1).ToInt();
								if (num69 == 0)
								{
									num69 = -WorldGen.ShiftTrapdoor(i, j, false, -1).ToInt();
								}
								if (num69 != 0)
								{
									NetMessage.SendData(19, -1, -1, null, 3 - value.ToInt(), (float)i, (float)j, (float)num69, 0, 0, 0);
									return;
								}
							}
							else
							{
								if (type == 389 || type == 388)
								{
									bool flag4 = type == 389;
									WorldGen.ShiftTallGate(i, j, flag4);
									NetMessage.SendData(19, -1, -1, null, 4 + flag4.ToInt(), (float)i, (float)j, 0f, 0, 0, 0);
									return;
								}
								if (TileLoader.CloseDoorID(Main.tile[i, j]) >= 0)
								{
									if (WorldGen.CloseDoor(i, j, true))
									{
										NetMessage.SendData(19, -1, -1, null, 1, (float)i, (float)j, 0f, 0, 0, 0);
										return;
									}
								}
								else if (TileLoader.OpenDoorID(Main.tile[i, j]) >= 0)
								{
									int num70 = 1;
									if (Main.rand.Next(2) == 0)
									{
										num70 = -1;
									}
									if (WorldGen.OpenDoor(i, j, num70))
									{
										NetMessage.SendData(19, -1, -1, null, 0, (float)i, (float)j, (float)num70, 0, 0, 0);
										return;
									}
									if (WorldGen.OpenDoor(i, j, -num70))
									{
										NetMessage.SendData(19, -1, -1, null, 0, (float)i, (float)j, -(float)num70, 0, 0, 0);
										return;
									}
								}
								else
								{
									if (type == 216)
									{
										WorldGen.LaunchRocket(i, j);
										Wiring.SkipWire(i, j);
										return;
									}
									if (type == 335)
									{
										int num71 = j - (int)(tile.frameY / 18);
										int num72 = i - (int)(tile.frameX / 18);
										Wiring.SkipWire(num72, num71);
										Wiring.SkipWire(num72, num71 + 1);
										Wiring.SkipWire(num72 + 1, num71);
										Wiring.SkipWire(num72 + 1, num71 + 1);
										if (Wiring.CheckMech(num72, num71, 30))
										{
											WorldGen.LaunchRocketSmall(num72, num71);
											return;
										}
									}
									else if (type == 338)
									{
										int num73 = j - (int)(tile.frameY / 18);
										int num74 = i - (int)(tile.frameX / 18);
										Wiring.SkipWire(num74, num73);
										Wiring.SkipWire(num74, num73 + 1);
										if (Wiring.CheckMech(num74, num73, 30))
										{
											bool flag5 = false;
											int num7;
											for (int num75 = 0; num75 < 1000; num75 = num7 + 1)
											{
												if (Main.projectile[num75].active && Main.projectile[num75].aiStyle == 73 && Main.projectile[num75].ai[0] == (float)num74 && Main.projectile[num75].ai[1] == (float)num73)
												{
													flag5 = true;
													break;
												}
												num7 = num75;
											}
											if (!flag5)
											{
												Projectile.NewProjectile((float)(num74 * 16 + 8), (float)(num73 * 16 + 2), 0f, 0f, 419 + Main.rand.Next(4), 0, 0f, Main.myPlayer, (float)num74, (float)num73);
												return;
											}
										}
									}
									else if (type == 235)
									{
										int num76 = i - (int)(tile.frameX / 18);
										if (tile.wall == 87 && (double)j > Main.worldSurface && !NPC.downedPlantBoss)
										{
											return;
										}
										if (Wiring._teleport[0].X == -1f)
										{
											Wiring._teleport[0].X = (float)num76;
											Wiring._teleport[0].Y = (float)j;
											if (tile.halfBrick())
											{
												Vector2[] teleport = Wiring._teleport;
												int num77 = 0;
												teleport[num77].Y = teleport[num77].Y + 0.5f;
												return;
											}
										}
										else if (Wiring._teleport[0].X != (float)num76 || Wiring._teleport[0].Y != (float)j)
										{
											Wiring._teleport[1].X = (float)num76;
											Wiring._teleport[1].Y = (float)j;
											if (tile.halfBrick())
											{
												Vector2[] teleport2 = Wiring._teleport;
												int num78 = 1;
												teleport2[num78].Y = teleport2[num78].Y + 0.5f;
												return;
											}
										}
									}
									else
									{
										if (TileLoader.IsTorch(type))
										{
											if (tile.frameX < 66)
											{
												Tile tile5 = tile;
												Tile tile3 = tile5;
												tile3.frameX += 66;
											}
											else
											{
												Tile tile6 = tile;
												Tile tile3 = tile6;
												tile3.frameX -= 66;
											}
											NetMessage.SendTileSquare(-1, i, j, 1, TileChangeType.None);
											return;
										}
										if (type == 429)
										{
											int num79 = (int)(Main.tile[i, j].frameX / 18);
											bool flag6 = num79 % 2 >= 1;
											bool flag7 = num79 % 4 >= 2;
											bool flag8 = num79 % 8 >= 4;
											bool flag9 = num79 % 16 >= 8;
											bool flag10 = false;
											short num80 = 0;
											switch (Wiring._currentWireColor)
											{
											case 1:
												num80 = 18;
												flag10 = !flag6;
												break;
											case 2:
												num80 = 72;
												flag10 = !flag8;
												break;
											case 3:
												num80 = 36;
												flag10 = !flag7;
												break;
											case 4:
												num80 = 144;
												flag10 = !flag9;
												break;
											}
											if (flag10)
											{
												Tile tile7 = tile;
												Tile tile3 = tile7;
												tile3.frameX += num80;
											}
											else
											{
												Tile tile8 = tile;
												Tile tile3 = tile8;
												tile3.frameX -= num80;
											}
											NetMessage.SendTileSquare(-1, i, j, 1, TileChangeType.None);
											return;
										}
										if (type == 149)
										{
											if (tile.frameX < 54)
											{
												Tile tile9 = tile;
												Tile tile3 = tile9;
												tile3.frameX += 54;
											}
											else
											{
												Tile tile10 = tile;
												Tile tile3 = tile10;
												tile3.frameX -= 54;
											}
											NetMessage.SendTileSquare(-1, i, j, 1, TileChangeType.None);
											return;
										}
										if (type == 244)
										{
											int num81;
											for (num81 = (int)(tile.frameX / 18); num81 >= 3; num81 -= 3)
											{
											}
											int num82;
											for (num82 = (int)(tile.frameY / 18); num82 >= 3; num82 -= 3)
											{
											}
											int num83 = i - num81;
											int num84 = j - num82;
											int num85 = 54;
											if (Main.tile[num83, num84].frameX >= 54)
											{
												num85 = -54;
											}
											int num7;
											for (int num86 = num83; num86 < num83 + 3; num86 = num7 + 1)
											{
												for (int num87 = num84; num87 < num84 + 2; num87 = num7 + 1)
												{
													Wiring.SkipWire(num86, num87);
													Main.tile[num86, num87].frameX = (short)((int)Main.tile[num86, num87].frameX + num85);
													num7 = num87;
												}
												num7 = num86;
											}
											NetMessage.SendTileSquare(-1, num83 + 1, num84 + 1, 3, TileChangeType.None);
											return;
										}
										if (type == 42)
										{
											int num88;
											for (num88 = (int)(tile.frameY / 18); num88 >= 2; num88 -= 2)
											{
											}
											int num89 = j - num88;
											short num90 = 18;
											if (tile.frameX > 0)
											{
												num90 = -18;
											}
											Tile tile11 = Main.tile[i, num89];
											Tile tile3 = tile11;
											tile3.frameX += num90;
											Tile tile12 = Main.tile[i, num89 + 1];
											tile3 = tile12;
											tile3.frameX += num90;
											Wiring.SkipWire(i, num89);
											Wiring.SkipWire(i, num89 + 1);
											NetMessage.SendTileSquare(-1, i, j, 2, TileChangeType.None);
											return;
										}
										if (type == 93)
										{
											int num91;
											for (num91 = (int)(tile.frameY / 18); num91 >= 3; num91 -= 3)
											{
											}
											num91 = j - num91;
											short num92 = 18;
											if (tile.frameX > 0)
											{
												num92 = -18;
											}
											Tile tile13 = Main.tile[i, num91];
											Tile tile3 = tile13;
											tile3.frameX += num92;
											Tile tile14 = Main.tile[i, num91 + 1];
											tile3 = tile14;
											tile3.frameX += num92;
											Tile tile15 = Main.tile[i, num91 + 2];
											tile3 = tile15;
											tile3.frameX += num92;
											Wiring.SkipWire(i, num91);
											Wiring.SkipWire(i, num91 + 1);
											Wiring.SkipWire(i, num91 + 2);
											NetMessage.SendTileSquare(-1, i, num91 + 1, 3, TileChangeType.None);
											return;
										}
										if (type == 126 || type == 95 || type == 100 || type == 173)
										{
											int num93;
											for (num93 = (int)(tile.frameY / 18); num93 >= 2; num93 -= 2)
											{
											}
											num93 = j - num93;
											int num94 = (int)(tile.frameX / 18);
											if (num94 > 1)
											{
												num94 -= 2;
											}
											num94 = i - num94;
											short num95 = 36;
											if (Main.tile[num94, num93].frameX > 0)
											{
												num95 = -36;
											}
											Tile tile16 = Main.tile[num94, num93];
											Tile tile3 = tile16;
											tile3.frameX += num95;
											Tile tile17 = Main.tile[num94, num93 + 1];
											tile3 = tile17;
											tile3.frameX += num95;
											Tile tile18 = Main.tile[num94 + 1, num93];
											tile3 = tile18;
											tile3.frameX += num95;
											Tile tile19 = Main.tile[num94 + 1, num93 + 1];
											tile3 = tile19;
											tile3.frameX += num95;
											Wiring.SkipWire(num94, num93);
											Wiring.SkipWire(num94 + 1, num93);
											Wiring.SkipWire(num94, num93 + 1);
											Wiring.SkipWire(num94 + 1, num93 + 1);
											NetMessage.SendTileSquare(-1, num94, num93, 3, TileChangeType.None);
											return;
										}
										if (type == 34)
										{
											int num96;
											for (num96 = (int)(tile.frameY / 18); num96 >= 3; num96 -= 3)
											{
											}
											int num97 = j - num96;
											int num98 = (int)(tile.frameX % 108 / 18);
											if (num98 > 2)
											{
												num98 -= 3;
											}
											num98 = i - num98;
											short num99 = 54;
											if (Main.tile[num98, num97].frameX % 108 > 0)
											{
												num99 = -54;
											}
											int num7;
											for (int num100 = num98; num100 < num98 + 3; num100 = num7 + 1)
											{
												for (int num101 = num97; num101 < num97 + 3; num101 = num7 + 1)
												{
													Tile tile20 = Main.tile[num100, num101];
													Tile tile3 = tile20;
													tile3.frameX += num99;
													Wiring.SkipWire(num100, num101);
													num7 = num101;
												}
												num7 = num100;
											}
											NetMessage.SendTileSquare(-1, num98 + 1, num97 + 1, 3, TileChangeType.None);
											return;
										}
										if (type == 314)
										{
											if (Wiring.CheckMech(i, j, 5))
											{
												Minecart.FlipSwitchTrack(i, j);
												return;
											}
										}
										else
										{
											if (type == 33 || type == 174)
											{
												short num102 = 18;
												if (tile.frameX > 0)
												{
													num102 = -18;
												}
												Tile tile21 = tile;
												Tile tile3 = tile21;
												tile3.frameX += num102;
												NetMessage.SendTileSquare(-1, i, j, 3, TileChangeType.None);
												return;
											}
											if (type == 92)
											{
												int num103 = j - (int)(tile.frameY / 18);
												short num104 = 18;
												if (tile.frameX > 0)
												{
													num104 = -18;
												}
												int num7;
												for (int num105 = num103; num105 < num103 + 6; num105 = num7 + 1)
												{
													Tile tile22 = Main.tile[i, num105];
													Tile tile3 = tile22;
													tile3.frameX += num104;
													Wiring.SkipWire(i, num105);
													num7 = num105;
												}
												NetMessage.SendTileSquare(-1, i, num103 + 3, 7, TileChangeType.None);
												return;
											}
											if (type == 137)
											{
												int num106 = (int)(tile.frameY / 18);
												Vector2 zero = Vector2.Zero;
												float speedX = 0f;
												float speedY = 0f;
												int num107 = 0;
												int damage2 = 0;
												switch (num106)
												{
												case 0:
												case 1:
												case 2:
													if (Wiring.CheckMech(i, j, 200))
													{
														int num108 = (tile.frameX == 0) ? -1 : ((tile.frameX == 18) ? 1 : 0);
														int num109 = (tile.frameX < 36) ? 0 : ((tile.frameX < 72) ? -1 : 1);
														zero = new Vector2((float)(i * 16 + 8 + 10 * num108), (float)(j * 16 + 9 + num109 * 9));
														float num110 = 3f;
														if (num106 == 0)
														{
															num107 = 98;
															damage2 = 20;
															num110 = 12f;
														}
														if (num106 == 1)
														{
															num107 = 184;
															damage2 = 40;
															num110 = 12f;
														}
														if (num106 == 2)
														{
															num107 = 187;
															damage2 = 40;
															num110 = 5f;
														}
														speedX = (float)num108 * num110;
														speedY = (float)num109 * num110;
													}
													break;
												case 3:
													if (Wiring.CheckMech(i, j, 300))
													{
														int num111 = 200;
														int num7;
														for (int num112 = 0; num112 < 1000; num112 = num7 + 1)
														{
															if (Main.projectile[num112].active && Main.projectile[num112].type == num107)
															{
																float num113 = (new Vector2((float)(i * 16 + 8), (float)(j * 18 + 8)) - Main.projectile[num112].Center).Length();
																if (num113 < 50f)
																{
																	num111 -= 50;
																}
																else if (num113 < 100f)
																{
																	num111 -= 15;
																}
																else if (num113 < 200f)
																{
																	num111 -= 10;
																}
																else if (num113 < 300f)
																{
																	num111 -= 8;
																}
																else if (num113 < 400f)
																{
																	num111 -= 6;
																}
																else if (num113 < 500f)
																{
																	num111 -= 5;
																}
																else if (num113 < 700f)
																{
																	num111 -= 4;
																}
																else if (num113 < 900f)
																{
																	num111 -= 3;
																}
																else if (num113 < 1200f)
																{
																	num111 -= 2;
																}
																else
																{
																	num7 = num111;
																	num111 = num7 - 1;
																}
															}
															num7 = num112;
														}
														if (num111 > 0)
														{
															num107 = 185;
															damage2 = 40;
															int num114 = 0;
															int num115 = 0;
															switch (tile.frameX / 18)
															{
															case 0:
															case 1:
																num114 = 0;
																num115 = 1;
																break;
															case 2:
																num114 = 0;
																num115 = -1;
																break;
															case 3:
																num114 = -1;
																num115 = 0;
																break;
															case 4:
																num114 = 1;
																num115 = 0;
																break;
															}
															speedX = (float)(4 * num114) + (float)Main.rand.Next(-20 + ((num114 == 1) ? 20 : 0), 21 - ((num114 == -1) ? 20 : 0)) * 0.05f;
															speedY = (float)(4 * num115) + (float)Main.rand.Next(-20 + ((num115 == 1) ? 20 : 0), 21 - ((num115 == -1) ? 20 : 0)) * 0.05f;
															zero = new Vector2((float)(i * 16 + 8 + 14 * num114), (float)(j * 16 + 8 + 14 * num115));
														}
													}
													break;
												case 4:
													if (Wiring.CheckMech(i, j, 90))
													{
														int num116 = 0;
														int num117 = 0;
														switch (tile.frameX / 18)
														{
														case 0:
														case 1:
															num116 = 0;
															num117 = 1;
															break;
														case 2:
															num116 = 0;
															num117 = -1;
															break;
														case 3:
															num116 = -1;
															num117 = 0;
															break;
														case 4:
															num116 = 1;
															num117 = 0;
															break;
														}
														speedX = (float)(8 * num116);
														speedY = (float)(8 * num117);
														damage2 = 60;
														num107 = 186;
														zero = new Vector2((float)(i * 16 + 8 + 18 * num116), (float)(j * 16 + 8 + 18 * num117));
													}
													break;
												}
												switch (num106 + 10)
												{
												case 0:
													if (Wiring.CheckMech(i, j, 200))
													{
														int num118 = -1;
														if (tile.frameX != 0)
														{
															num118 = 1;
														}
														speedX = (float)(12 * num118);
														damage2 = 20;
														num107 = 98;
														zero = new Vector2((float)(i * 16 + 8), (float)(j * 16 + 7));
														zero.X += (float)(10 * num118);
														zero.Y += 2f;
													}
													break;
												case 1:
													if (Wiring.CheckMech(i, j, 200))
													{
														int num119 = -1;
														if (tile.frameX != 0)
														{
															num119 = 1;
														}
														speedX = (float)(12 * num119);
														damage2 = 40;
														num107 = 184;
														zero = new Vector2((float)(i * 16 + 8), (float)(j * 16 + 7));
														zero.X += (float)(10 * num119);
														zero.Y += 2f;
													}
													break;
												case 2:
													if (Wiring.CheckMech(i, j, 200))
													{
														int num120 = -1;
														if (tile.frameX != 0)
														{
															num120 = 1;
														}
														speedX = (float)(5 * num120);
														damage2 = 40;
														num107 = 187;
														zero = new Vector2((float)(i * 16 + 8), (float)(j * 16 + 7));
														zero.X += (float)(10 * num120);
														zero.Y += 2f;
													}
													break;
												case 3:
													if (Wiring.CheckMech(i, j, 300))
													{
														num107 = 185;
														int num121 = 200;
														int num7;
														for (int num122 = 0; num122 < 1000; num122 = num7 + 1)
														{
															if (Main.projectile[num122].active && Main.projectile[num122].type == num107)
															{
																float num123 = (new Vector2((float)(i * 16 + 8), (float)(j * 18 + 8)) - Main.projectile[num122].Center).Length();
																if (num123 < 50f)
																{
																	num121 -= 50;
																}
																else if (num123 < 100f)
																{
																	num121 -= 15;
																}
																else if (num123 < 200f)
																{
																	num121 -= 10;
																}
																else if (num123 < 300f)
																{
																	num121 -= 8;
																}
																else if (num123 < 400f)
																{
																	num121 -= 6;
																}
																else if (num123 < 500f)
																{
																	num121 -= 5;
																}
																else if (num123 < 700f)
																{
																	num121 -= 4;
																}
																else if (num123 < 900f)
																{
																	num121 -= 3;
																}
																else if (num123 < 1200f)
																{
																	num121 -= 2;
																}
																else
																{
																	num7 = num121;
																	num121 = num7 - 1;
																}
															}
															num7 = num122;
														}
														if (num121 > 0)
														{
															speedX = (float)Main.rand.Next(-20, 21) * 0.05f;
															speedY = 4f + (float)Main.rand.Next(0, 21) * 0.05f;
															damage2 = 40;
															zero = new Vector2((float)(i * 16 + 8), (float)(j * 16 + 16));
															zero.Y += 6f;
															Projectile.NewProjectile((float)((int)zero.X), (float)((int)zero.Y), speedX, speedY, num107, damage2, 2f, Main.myPlayer, 0f, 0f);
														}
													}
													break;
												case 4:
													if (Wiring.CheckMech(i, j, 90))
													{
														speedX = 0f;
														speedY = 8f;
														damage2 = 60;
														num107 = 186;
														zero = new Vector2((float)(i * 16 + 8), (float)(j * 16 + 16));
														zero.Y += 10f;
													}
													break;
												}
												if (num107 != 0)
												{
													Projectile.NewProjectile((float)((int)zero.X), (float)((int)zero.Y), speedX, speedY, num107, damage2, 2f, Main.myPlayer, 0f, 0f);
													return;
												}
											}
											else if (type == 443)
											{
												int num124 = (int)(tile.frameX / 36);
												int num125 = i - ((int)tile.frameX - num124 * 36) / 18;
												if (Wiring.CheckMech(num125, j, 200))
												{
													Vector2 vector2 = Vector2.Zero;
													Vector2 zero2 = Vector2.Zero;
													int num126 = 654;
													int damage3 = 20;
													if (num124 < 2)
													{
														vector2 = new Vector2((float)(num125 + 1), (float)j) * 16f;
														zero2 = new Vector2(0f, -8f);
													}
													else
													{
														vector2 = new Vector2((float)(num125 + 1), (float)(j + 1)) * 16f;
														zero2 = new Vector2(0f, 8f);
													}
													if (num126 != 0)
													{
														Projectile.NewProjectile((float)((int)vector2.X), (float)((int)vector2.Y), zero2.X, zero2.Y, num126, damage3, 2f, Main.myPlayer, 0f, 0f);
														return;
													}
												}
											}
											else
											{
												if (type == 139 || type == 35 || TileLoader.IsModMusicBox(tile))
												{
													WorldGen.SwitchMB(i, j);
													return;
												}
												if (type == 207)
												{
													WorldGen.SwitchFountain(i, j);
													return;
												}
												if (type == 410)
												{
													WorldGen.SwitchMonolith(i, j);
													return;
												}
												if (type == 455)
												{
													BirthdayParty.ToggleManualParty();
													return;
												}
												if (type == 141)
												{
													WorldGen.KillTile(i, j, false, false, true);
													NetMessage.SendTileSquare(-1, i, j, 1, TileChangeType.None);
													Projectile.NewProjectile((float)(i * 16 + 8), (float)(j * 16 + 8), 0f, 0f, 108, 500, 10f, Main.myPlayer, 0f, 0f);
													return;
												}
												if (type == 210)
												{
													WorldGen.ExplodeMine(i, j);
													return;
												}
												if (type == 142 || type == 143)
												{
													int num127 = j - (int)(tile.frameY / 18);
													int num128 = (int)(tile.frameX / 18);
													if (num128 > 1)
													{
														num128 -= 2;
													}
													num128 = i - num128;
													Wiring.SkipWire(num128, num127);
													Wiring.SkipWire(num128, num127 + 1);
													Wiring.SkipWire(num128 + 1, num127);
													Wiring.SkipWire(num128 + 1, num127 + 1);
													int num7;
													if (type == 142)
													{
														for (int num129 = 0; num129 < 4; num129 = num7 + 1)
														{
															if (Wiring._numInPump >= 19)
															{
																return;
															}
															int num130;
															int num131;
															if (num129 == 0)
															{
																num130 = num128;
																num131 = num127 + 1;
															}
															else if (num129 == 1)
															{
																num130 = num128 + 1;
																num131 = num127 + 1;
															}
															else if (num129 == 2)
															{
																num130 = num128;
																num131 = num127;
															}
															else
															{
																num130 = num128 + 1;
																num131 = num127;
															}
															Wiring._inPumpX[Wiring._numInPump] = num130;
															Wiring._inPumpY[Wiring._numInPump] = num131;
															num7 = Wiring._numInPump;
															Wiring._numInPump = num7 + 1;
															num7 = num129;
														}
														return;
													}
													for (int num132 = 0; num132 < 4; num132 = num7 + 1)
													{
														if (Wiring._numOutPump >= 19)
														{
															return;
														}
														int num133;
														int num134;
														if (num132 == 0)
														{
															num133 = num128;
															num134 = num127 + 1;
														}
														else if (num132 == 1)
														{
															num133 = num128 + 1;
															num134 = num127 + 1;
														}
														else if (num132 == 2)
														{
															num133 = num128;
															num134 = num127;
														}
														else
														{
															num133 = num128 + 1;
															num134 = num127;
														}
														Wiring._outPumpX[Wiring._numOutPump] = num133;
														Wiring._outPumpY[Wiring._numOutPump] = num134;
														num7 = Wiring._numOutPump;
														Wiring._numOutPump = num7 + 1;
														num7 = num132;
													}
													return;
												}
												else if (type == 105)
												{
													int num135 = j - (int)(tile.frameY / 18);
													int num136 = (int)(tile.frameX / 18);
													int num137 = 0;
													while (num136 >= 2)
													{
														num136 -= 2;
														int num7 = num137;
														num137 = num7 + 1;
													}
													num136 = i - num136;
													num136 = i - (int)(tile.frameX % 36 / 18);
													num135 = j - (int)(tile.frameY % 54 / 18);
													num137 = (int)(tile.frameX / 36 + tile.frameY / 54 * 55);
													Wiring.SkipWire(num136, num135);
													Wiring.SkipWire(num136, num135 + 1);
													Wiring.SkipWire(num136, num135 + 2);
													Wiring.SkipWire(num136 + 1, num135);
													Wiring.SkipWire(num136 + 1, num135 + 1);
													Wiring.SkipWire(num136 + 1, num135 + 2);
													int num138 = num136 * 16 + 16;
													int num139 = (num135 + 3) * 16;
													int num140 = -1;
													int num141 = -1;
													bool flag11 = true;
													bool flag12 = false;
													switch (num137)
													{
													case 51:
														num141 = (int)Utils.SelectRandom<short>(Main.rand, new short[]
														{
															299,
															538
														});
														break;
													case 52:
														num141 = 356;
														break;
													case 53:
														num141 = 357;
														break;
													case 54:
														num141 = (int)Utils.SelectRandom<short>(Main.rand, new short[]
														{
															355,
															358
														});
														break;
													case 55:
														num141 = (int)Utils.SelectRandom<short>(Main.rand, new short[]
														{
															367,
															366
														});
														break;
													case 56:
														num141 = (int)Utils.SelectRandom<short>(Main.rand, new short[]
														{
															359,
															359,
															359,
															359,
															360
														});
														break;
													case 57:
														num141 = 377;
														break;
													case 58:
														num141 = 300;
														break;
													case 59:
														num141 = (int)Utils.SelectRandom<short>(Main.rand, new short[]
														{
															364,
															362
														});
														break;
													case 60:
														num141 = 148;
														break;
													case 61:
														num141 = 361;
														break;
													case 62:
														num141 = (int)Utils.SelectRandom<short>(Main.rand, new short[]
														{
															487,
															486,
															485
														});
														break;
													case 63:
														num141 = 164;
														flag11 &= NPC.MechSpawn((float)num138, (float)num139, 165);
														break;
													case 64:
														num141 = 86;
														flag12 = true;
														break;
													case 65:
														num141 = 490;
														break;
													case 66:
														num141 = 82;
														break;
													case 67:
														num141 = 449;
														break;
													case 68:
														num141 = 167;
														break;
													case 69:
														num141 = 480;
														break;
													case 70:
														num141 = 48;
														break;
													case 71:
														num141 = (int)Utils.SelectRandom<short>(Main.rand, new short[]
														{
															170,
															180,
															171
														});
														flag12 = true;
														break;
													case 72:
														num141 = 481;
														break;
													case 73:
														num141 = 482;
														break;
													case 74:
														num141 = 430;
														break;
													case 75:
														num141 = 489;
														break;
													}
													if (num141 != -1 && Wiring.CheckMech(num136, num135, 30) && NPC.MechSpawn((float)num138, (float)num139, num141) && flag11)
													{
														if (!flag12 || !Collision.SolidTiles(num136 - 2, num136 + 3, num135, num135 + 2))
														{
															num140 = NPC.NewNPC(num138, num139 - 12, num141, 0, 0f, 0f, 0f, 0f, 255);
														}
														else
														{
															Vector2 vector3 = new Vector2((float)(num138 - 4), (float)(num139 - 22)) - new Vector2(10f);
															Utils.PoofOfSmoke(vector3);
															NetMessage.SendData(106, -1, -1, null, (int)vector3.X, vector3.Y, 0f, 0f, 0, 0, 0);
														}
													}
													if (num140 <= -1)
													{
														if (num137 == 4)
														{
															if (Wiring.CheckMech(num136, num135, 30) && NPC.MechSpawn((float)num138, (float)num139, 1))
															{
																num140 = NPC.NewNPC(num138, num139 - 12, 1, 0, 0f, 0f, 0f, 0f, 255);
															}
														}
														else if (num137 == 7)
														{
															if (Wiring.CheckMech(num136, num135, 30) && NPC.MechSpawn((float)num138, (float)num139, 49))
															{
																num140 = NPC.NewNPC(num138 - 4, num139 - 6, 49, 0, 0f, 0f, 0f, 0f, 255);
															}
														}
														else if (num137 == 8)
														{
															if (Wiring.CheckMech(num136, num135, 30) && NPC.MechSpawn((float)num138, (float)num139, 55))
															{
																num140 = NPC.NewNPC(num138, num139 - 12, 55, 0, 0f, 0f, 0f, 0f, 255);
															}
														}
														else if (num137 == 9)
														{
															if (Wiring.CheckMech(num136, num135, 30) && NPC.MechSpawn((float)num138, (float)num139, 46))
															{
																num140 = NPC.NewNPC(num138, num139 - 12, 46, 0, 0f, 0f, 0f, 0f, 255);
															}
														}
														else if (num137 == 10)
														{
															if (Wiring.CheckMech(num136, num135, 30) && NPC.MechSpawn((float)num138, (float)num139, 21))
															{
																num140 = NPC.NewNPC(num138, num139, 21, 0, 0f, 0f, 0f, 0f, 255);
															}
														}
														else if (num137 == 18)
														{
															if (Wiring.CheckMech(num136, num135, 30) && NPC.MechSpawn((float)num138, (float)num139, 67))
															{
																num140 = NPC.NewNPC(num138, num139 - 12, 67, 0, 0f, 0f, 0f, 0f, 255);
															}
														}
														else if (num137 == 23)
														{
															if (Wiring.CheckMech(num136, num135, 30) && NPC.MechSpawn((float)num138, (float)num139, 63))
															{
																num140 = NPC.NewNPC(num138, num139 - 12, 63, 0, 0f, 0f, 0f, 0f, 255);
															}
														}
														else if (num137 == 27)
														{
															if (Wiring.CheckMech(num136, num135, 30) && NPC.MechSpawn((float)num138, (float)num139, 85))
															{
																num140 = NPC.NewNPC(num138 - 9, num139, 85, 0, 0f, 0f, 0f, 0f, 255);
															}
														}
														else if (num137 == 28)
														{
															if (Wiring.CheckMech(num136, num135, 30) && NPC.MechSpawn((float)num138, (float)num139, 74))
															{
																num140 = NPC.NewNPC(num138, num139 - 12, (int)Utils.SelectRandom<short>(Main.rand, new short[]
																{
																	74,
																	297,
																	298
																}), 0, 0f, 0f, 0f, 0f, 255);
															}
														}
														else if (num137 == 34)
														{
															int num7;
															for (int num142 = 0; num142 < 2; num142 = num7 + 1)
															{
																for (int num143 = 0; num143 < 3; num143 = num7 + 1)
																{
																	Tile tile23 = Main.tile[num136 + num142, num135 + num143];
																	tile23.type = 349;
																	tile23.frameX = (short)(num142 * 18 + 216);
																	tile23.frameY = (short)(num143 * 18);
																	num7 = num143;
																}
																num7 = num142;
															}
															Animation.NewTemporaryAnimation(0, 349, num136, num135);
															if (Main.netMode == 2)
															{
																NetMessage.SendTileRange(-1, num136, num135, 2, 3, TileChangeType.None);
															}
														}
														else if (num137 == 42)
														{
															if (Wiring.CheckMech(num136, num135, 30) && NPC.MechSpawn((float)num138, (float)num139, 58))
															{
																num140 = NPC.NewNPC(num138, num139 - 12, 58, 0, 0f, 0f, 0f, 0f, 255);
															}
														}
														else if (num137 == 37)
														{
															if (Wiring.CheckMech(num136, num135, 600) && Item.MechSpawn((float)num138, (float)num139, 58) && Item.MechSpawn((float)num138, (float)num139, 1734) && Item.MechSpawn((float)num138, (float)num139, 1867))
															{
																Item.NewItem(num138, num139 - 16, 0, 0, 58, 1, false, 0, false, false);
															}
														}
														else if (num137 == 50)
														{
															if (Wiring.CheckMech(num136, num135, 30) && NPC.MechSpawn((float)num138, (float)num139, 65))
															{
																if (!Collision.SolidTiles(num136 - 2, num136 + 3, num135, num135 + 2))
																{
																	num140 = NPC.NewNPC(num138, num139 - 12, 65, 0, 0f, 0f, 0f, 0f, 255);
																}
																else
																{
																	Vector2 vector4 = new Vector2((float)(num138 - 4), (float)(num139 - 22)) - new Vector2(10f);
																	Utils.PoofOfSmoke(vector4);
																	NetMessage.SendData(106, -1, -1, null, (int)vector4.X, vector4.Y, 0f, 0f, 0, 0, 0);
																}
															}
														}
														else if (num137 == 2)
														{
															if (Wiring.CheckMech(num136, num135, 600) && Item.MechSpawn((float)num138, (float)num139, 184) && Item.MechSpawn((float)num138, (float)num139, 1735) && Item.MechSpawn((float)num138, (float)num139, 1868))
															{
																Item.NewItem(num138, num139 - 16, 0, 0, 184, 1, false, 0, false, false);
															}
														}
														else if (num137 == 17)
														{
															if (Wiring.CheckMech(num136, num135, 600) && Item.MechSpawn((float)num138, (float)num139, 166))
															{
																Item.NewItem(num138, num139 - 20, 0, 0, 166, 1, false, 0, false, false);
															}
														}
														else if (num137 == 40)
														{
															if (Wiring.CheckMech(num136, num135, 300))
															{
																int[] array = new int[10];
																int num144 = 0;
																int num7;
																for (int num145 = 0; num145 < 200; num145 = num7 + 1)
																{
																	if (Main.npc[num145].active && (Main.npc[num145].type == 17 || Main.npc[num145].type == 19 || Main.npc[num145].type == 22 || Main.npc[num145].type == 38 || Main.npc[num145].type == 54 || Main.npc[num145].type == 107 || Main.npc[num145].type == 108 || Main.npc[num145].type == 142 || Main.npc[num145].type == 160 || Main.npc[num145].type == 207 || Main.npc[num145].type == 209 || Main.npc[num145].type == 227 || Main.npc[num145].type == 228 || Main.npc[num145].type == 229 || Main.npc[num145].type == 358 || Main.npc[num145].type == 369 || Main.npc[num145].type == 550))
																	{
																		array[num144] = num145;
																		num7 = num144;
																		num144 = num7 + 1;
																		if (num144 >= 9)
																		{
																			break;
																		}
																	}
																	num7 = num145;
																}
																if (num144 > 0)
																{
																	int num146 = array[Main.rand.Next(num144)];
																	Main.npc[num146].position.X = (float)(num138 - Main.npc[num146].width / 2);
																	Main.npc[num146].position.Y = (float)(num139 - Main.npc[num146].height - 1);
																	NetMessage.SendData(23, -1, -1, null, num146, 0f, 0f, 0f, 0, 0, 0);
																}
															}
														}
														else if (num137 == 41 && Wiring.CheckMech(num136, num135, 300))
														{
															int[] array2 = new int[10];
															int num147 = 0;
															int num7;
															for (int num148 = 0; num148 < 200; num148 = num7 + 1)
															{
																if (Main.npc[num148].active && (Main.npc[num148].type == 18 || Main.npc[num148].type == 20 || Main.npc[num148].type == 124 || Main.npc[num148].type == 178 || Main.npc[num148].type == 208 || Main.npc[num148].type == 353))
																{
																	array2[num147] = num148;
																	num7 = num147;
																	num147 = num7 + 1;
																	if (num147 >= 9)
																	{
																		break;
																	}
																}
																num7 = num148;
															}
															if (num147 > 0)
															{
																int num149 = array2[Main.rand.Next(num147)];
																Main.npc[num149].position.X = (float)(num138 - Main.npc[num149].width / 2);
																Main.npc[num149].position.Y = (float)(num139 - Main.npc[num149].height - 1);
																NetMessage.SendData(23, -1, -1, null, num149, 0f, 0f, 0f, 0, 0, 0);
															}
														}
													}
													if (num140 >= 0)
													{
														Main.npc[num140].value = 0f;
														Main.npc[num140].npcSlots = 0f;
														Main.npc[num140].SpawnedFromStatue = true;
														return;
													}
												}
												else if (type == 349)
												{
													int num150 = j - (int)(tile.frameY / 18);
													int num151;
													for (num151 = (int)(tile.frameX / 18); num151 >= 2; num151 -= 2)
													{
													}
													num151 = i - num151;
													Wiring.SkipWire(num151, num150);
													Wiring.SkipWire(num151, num150 + 1);
													Wiring.SkipWire(num151, num150 + 2);
													Wiring.SkipWire(num151 + 1, num150);
													Wiring.SkipWire(num151 + 1, num150 + 1);
													Wiring.SkipWire(num151 + 1, num150 + 2);
													short num152;
													if (Main.tile[num151, num150].frameX == 0)
													{
														num152 = 216;
													}
													else
													{
														num152 = -216;
													}
													int num7;
													for (int num153 = 0; num153 < 2; num153 = num7 + 1)
													{
														for (int num154 = 0; num154 < 3; num154 = num7 + 1)
														{
															Tile tile24 = Main.tile[num151 + num153, num150 + num154];
															Tile tile3 = tile24;
															tile3.frameX += num152;
															num7 = num154;
														}
														num7 = num153;
													}
													if (Main.netMode == 2)
													{
														NetMessage.SendTileRange(-1, num151, num150, 2, 3, TileChangeType.None);
													}
													Animation.NewTemporaryAnimation((num152 > 0) ? 0 : 1, 349, num151, num150);
												}
											}
										}
									}
								}
							}
						}
					}
				}
				TileLoader.HitWire(i, j, type);
			}
		}

		// Token: 0x060007DD RID: 2013 RVA: 0x00376430 File Offset: 0x00374630
		private static void Teleport()
		{
			if (Wiring._teleport[0].X < Wiring._teleport[1].X + 3f && Wiring._teleport[0].X > Wiring._teleport[1].X - 3f && Wiring._teleport[0].Y > Wiring._teleport[1].Y - 3f && Wiring._teleport[0].Y < Wiring._teleport[1].Y)
			{
				return;
			}
			Rectangle[] array = new Rectangle[2];
			array[0].X = (int)(Wiring._teleport[0].X * 16f);
			array[0].Width = 48;
			array[0].Height = 48;
			array[0].Y = (int)(Wiring._teleport[0].Y * 16f - (float)array[0].Height);
			array[1].X = (int)(Wiring._teleport[1].X * 16f);
			array[1].Width = 48;
			array[1].Height = 48;
			array[1].Y = (int)(Wiring._teleport[1].Y * 16f - (float)array[1].Height);
			for (int i = 0; i < 2; i++)
			{
				Vector2 value = new Vector2((float)(array[1].X - array[0].X), (float)(array[1].Y - array[0].Y));
				if (i == 1)
				{
					value = new Vector2((float)(array[0].X - array[1].X), (float)(array[0].Y - array[1].Y));
				}
				if (!Wiring.blockPlayerTeleportationForOneIteration)
				{
					for (int j = 0; j < 255; j++)
					{
						if (Main.player[j].active && !Main.player[j].dead && !Main.player[j].teleporting && array[i].Intersects(Main.player[j].getRect()))
						{
							Vector2 vector = Main.player[j].position + value;
							Main.player[j].teleporting = true;
							if (Main.netMode == 2)
							{
								RemoteClient.CheckSection(j, vector, 1);
							}
							Main.player[j].Teleport(vector, 0, 0);
							if (Main.netMode == 2)
							{
								NetMessage.SendData(65, -1, -1, null, 0, (float)j, vector.X, vector.Y, 0, 0, 0);
							}
						}
					}
				}
				for (int k = 0; k < 200; k++)
				{
					if (Main.npc[k].active && !Main.npc[k].teleporting && Main.npc[k].lifeMax > 5 && !Main.npc[k].boss && !Main.npc[k].noTileCollide)
					{
						int type = Main.npc[k].type;
						if (!NPCID.Sets.TeleportationImmune[type] && array[i].Intersects(Main.npc[k].getRect()))
						{
							Main.npc[k].teleporting = true;
							Main.npc[k].Teleport(Main.npc[k].position + value, 0, 0);
						}
					}
				}
			}
			for (int l = 0; l < 255; l++)
			{
				Main.player[l].teleporting = false;
			}
			for (int m = 0; m < 200; m++)
			{
				Main.npc[m].teleporting = false;
			}
		}

		// Token: 0x060007DE RID: 2014 RVA: 0x00376838 File Offset: 0x00374A38
		public static void DeActive(int i, int j)
		{
			if (!Main.tile[i, j].active())
			{
				return;
			}
			bool flag = Main.tileSolid[(int)Main.tile[i, j].type] && !TileID.Sets.NotReallySolid[(int)Main.tile[i, j].type];
			ushort type = Main.tile[i, j].type;
			if (type == 314 || type - 386 <= 3)
			{
				flag = false;
			}
			if (!flag)
			{
				return;
			}
			if (Main.tile[i, j - 1].active() && (Main.tile[i, j - 1].type == 5 || TileID.Sets.BasicChest[(int)Main.tile[i, j - 1].type] || Main.tile[i, j - 1].type == 26 || Main.tile[i, j - 1].type == 77 || Main.tile[i, j - 1].type == 72 || Main.tile[i, j - 1].type == 88))
			{
				return;
			}
			Main.tile[i, j].inActive(true);
			WorldGen.SquareTileFrame(i, j, false);
			if (Main.netMode != 1)
			{
				NetMessage.SendTileSquare(-1, i, j, 1, TileChangeType.None);
			}
		}

		// Token: 0x060007DF RID: 2015 RVA: 0x0000A337 File Offset: 0x00008537
		public static void ReActive(int i, int j)
		{
			Main.tile[i, j].inActive(false);
			WorldGen.SquareTileFrame(i, j, false);
			if (Main.netMode != 1)
			{
				NetMessage.SendTileSquare(-1, i, j, 1, TileChangeType.None);
			}
		}

		// Token: 0x060007E0 RID: 2016 RVA: 0x0037698C File Offset: 0x00374B8C
		private static void MassWireOperationInner(Point ps, Point pe, Vector2 dropPoint, bool dir, ref int wireCount, ref int actuatorCount)
		{
			Math.Abs(ps.X - pe.X);
			Math.Abs(ps.Y - pe.Y);
			int num = Math.Sign(pe.X - ps.X);
			int num2 = Math.Sign(pe.Y - ps.Y);
			WiresUI.Settings.MultiToolMode toolMode = WiresUI.Settings.ToolMode;
			Point pt = default(Point);
			bool flag = false;
			Item.StartCachingType(530);
			Item.StartCachingType(849);
			int num3;
			int num4;
			int num5;
			if (dir)
			{
				pt.X = ps.X;
				num3 = ps.Y;
				num4 = pe.Y;
				num5 = num2;
			}
			else
			{
				pt.Y = ps.Y;
				num3 = ps.X;
				num4 = pe.X;
				num5 = num;
			}
			int num6 = num3;
			while (num6 != num4 && !flag)
			{
				if (dir)
				{
					pt.Y = num6;
				}
				else
				{
					pt.X = num6;
				}
				bool? flag2 = Wiring.MassWireOperationStep(pt, toolMode, ref wireCount, ref actuatorCount);
				if (flag2 != null && !flag2.Value)
				{
					flag = true;
					break;
				}
				num6 += num5;
			}
			if (dir)
			{
				pt.Y = pe.Y;
				num3 = ps.X;
				num4 = pe.X;
				num5 = num;
			}
			else
			{
				pt.X = pe.X;
				num3 = ps.Y;
				num4 = pe.Y;
				num5 = num2;
			}
			int num7 = num3;
			while (num7 != num4 && !flag)
			{
				if (!dir)
				{
					pt.Y = num7;
				}
				else
				{
					pt.X = num7;
				}
				bool? flag3 = Wiring.MassWireOperationStep(pt, toolMode, ref wireCount, ref actuatorCount);
				if (flag3 != null && !flag3.Value)
				{
					flag = true;
					break;
				}
				num7 += num5;
			}
			if (!flag)
			{
				Wiring.MassWireOperationStep(pe, toolMode, ref wireCount, ref actuatorCount);
			}
			Item.DropCache(dropPoint, Vector2.Zero, 530, true);
			Item.DropCache(dropPoint, Vector2.Zero, 849, true);
		}

		// Token: 0x060007E1 RID: 2017 RVA: 0x00376B6C File Offset: 0x00374D6C
		private static bool? MassWireOperationStep(Point pt, WiresUI.Settings.MultiToolMode mode, ref int wiresLeftToConsume, ref int actuatorsLeftToConstume)
		{
			if (!WorldGen.InWorld(pt.X, pt.Y, 1))
			{
				return null;
			}
			Tile tile = Main.tile[pt.X, pt.Y];
			if (tile == null)
			{
				return null;
			}
			if (!mode.HasFlag(WiresUI.Settings.MultiToolMode.Cutter))
			{
				if (mode.HasFlag(WiresUI.Settings.MultiToolMode.Red) && !tile.wire())
				{
					if (wiresLeftToConsume <= 0)
					{
						return new bool?(false);
					}
					wiresLeftToConsume--;
					WorldGen.PlaceWire(pt.X, pt.Y);
					NetMessage.SendData(17, -1, -1, null, 5, (float)pt.X, (float)pt.Y, 0f, 0, 0, 0);
				}
				if (mode.HasFlag(WiresUI.Settings.MultiToolMode.Green) && !tile.wire3())
				{
					if (wiresLeftToConsume <= 0)
					{
						return new bool?(false);
					}
					wiresLeftToConsume--;
					WorldGen.PlaceWire3(pt.X, pt.Y);
					NetMessage.SendData(17, -1, -1, null, 12, (float)pt.X, (float)pt.Y, 0f, 0, 0, 0);
				}
				if (mode.HasFlag(WiresUI.Settings.MultiToolMode.Blue) && !tile.wire2())
				{
					if (wiresLeftToConsume <= 0)
					{
						return new bool?(false);
					}
					wiresLeftToConsume--;
					WorldGen.PlaceWire2(pt.X, pt.Y);
					NetMessage.SendData(17, -1, -1, null, 10, (float)pt.X, (float)pt.Y, 0f, 0, 0, 0);
				}
				if (mode.HasFlag(WiresUI.Settings.MultiToolMode.Yellow) && !tile.wire4())
				{
					if (wiresLeftToConsume <= 0)
					{
						return new bool?(false);
					}
					wiresLeftToConsume--;
					WorldGen.PlaceWire4(pt.X, pt.Y);
					NetMessage.SendData(17, -1, -1, null, 16, (float)pt.X, (float)pt.Y, 0f, 0, 0, 0);
				}
				if (mode.HasFlag(WiresUI.Settings.MultiToolMode.Actuator) && !tile.actuator())
				{
					if (actuatorsLeftToConstume <= 0)
					{
						return new bool?(false);
					}
					actuatorsLeftToConstume--;
					WorldGen.PlaceActuator(pt.X, pt.Y);
					NetMessage.SendData(17, -1, -1, null, 8, (float)pt.X, (float)pt.Y, 0f, 0, 0, 0);
				}
			}
			if (mode.HasFlag(WiresUI.Settings.MultiToolMode.Cutter))
			{
				if (mode.HasFlag(WiresUI.Settings.MultiToolMode.Red) && tile.wire() && WorldGen.KillWire(pt.X, pt.Y))
				{
					NetMessage.SendData(17, -1, -1, null, 6, (float)pt.X, (float)pt.Y, 0f, 0, 0, 0);
				}
				if (mode.HasFlag(WiresUI.Settings.MultiToolMode.Green) && tile.wire3() && WorldGen.KillWire3(pt.X, pt.Y))
				{
					NetMessage.SendData(17, -1, -1, null, 13, (float)pt.X, (float)pt.Y, 0f, 0, 0, 0);
				}
				if (mode.HasFlag(WiresUI.Settings.MultiToolMode.Blue) && tile.wire2() && WorldGen.KillWire2(pt.X, pt.Y))
				{
					NetMessage.SendData(17, -1, -1, null, 11, (float)pt.X, (float)pt.Y, 0f, 0, 0, 0);
				}
				if (mode.HasFlag(WiresUI.Settings.MultiToolMode.Yellow) && tile.wire4() && WorldGen.KillWire4(pt.X, pt.Y))
				{
					NetMessage.SendData(17, -1, -1, null, 17, (float)pt.X, (float)pt.Y, 0f, 0, 0, 0);
				}
				if (mode.HasFlag(WiresUI.Settings.MultiToolMode.Actuator) && tile.actuator() && WorldGen.KillActuator(pt.X, pt.Y))
				{
					NetMessage.SendData(17, -1, -1, null, 9, (float)pt.X, (float)pt.Y, 0f, 0, 0, 0);
				}
			}
			return new bool?(true);
		}

		// Token: 0x04000D40 RID: 3392
		private const int MaxPump = 20;

		// Token: 0x04000D41 RID: 3393
		private const int MaxMech = 1000;

		// Token: 0x04000D42 RID: 3394
		public static bool blockPlayerTeleportationForOneIteration;

		// Token: 0x04000D43 RID: 3395
		public static bool running;

		// Token: 0x04000D44 RID: 3396
		private static Dictionary<Point16, bool> _wireSkip;

		// Token: 0x04000D45 RID: 3397
		public static DoubleStack<Point16> _wireList;

		// Token: 0x04000D46 RID: 3398
		public static DoubleStack<byte> _wireDirectionList;

		// Token: 0x04000D47 RID: 3399
		public static Dictionary<Point16, byte> _toProcess;

		// Token: 0x04000D48 RID: 3400
		private static Queue<Point16> _GatesCurrent;

		// Token: 0x04000D49 RID: 3401
		public static Queue<Point16> _LampsToCheck;

		// Token: 0x04000D4A RID: 3402
		public static Queue<Point16> _GatesNext;

		// Token: 0x04000D4B RID: 3403
		private static Dictionary<Point16, bool> _GatesDone;

		// Token: 0x04000D4C RID: 3404
		private static Dictionary<Point16, byte> _PixelBoxTriggers;

		// Token: 0x04000D4D RID: 3405
		public static Vector2[] _teleport;

		// Token: 0x04000D4E RID: 3406
		public static int[] _inPumpX;

		// Token: 0x04000D4F RID: 3407
		public static int[] _inPumpY;

		// Token: 0x04000D50 RID: 3408
		public static int _numInPump;

		// Token: 0x04000D51 RID: 3409
		public static int[] _outPumpX;

		// Token: 0x04000D52 RID: 3410
		public static int[] _outPumpY;

		// Token: 0x04000D53 RID: 3411
		public static int _numOutPump;

		// Token: 0x04000D54 RID: 3412
		private static int[] _mechX;

		// Token: 0x04000D55 RID: 3413
		private static int[] _mechY;

		// Token: 0x04000D56 RID: 3414
		private static int _numMechs;

		// Token: 0x04000D57 RID: 3415
		private static int[] _mechTime;

		// Token: 0x04000D58 RID: 3416
		public static int _currentWireColor;

		// Token: 0x04000D59 RID: 3417
		private static int CurrentUser = 254;
	}
}
